package com.liang.cloud.Controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.liang.cloud.Emun.ResultEnum;
import com.liang.cloud.Entity.ProductCategory;
import com.liang.cloud.Entity.ProductInfo;
import com.liang.cloud.Handler.SellFackHandler;
import com.liang.cloud.Util.ResultVoUtil;
import com.liang.cloud.Vo.ResultVo;
import com.liang.cloud.Fegin.CategoryFeign;
import com.liang.cloud.Service.ProductInfoService;
import com.liang.cloud.Vo.ProductInfoVo;
import com.liang.cloud.Vo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: 余亮亮
 * @description:
 * @create: 2020-03-26 13:00:50
 **/
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductInfoService productInfoService;
    @Autowired
    CategoryFeign categoryFeign;
    @GetMapping("/list")
    public ResultVo<List<ProductInfo>> list(){
        List<ProductInfo> list = productInfoService.findUp();
        return ResultVoUtil.success(list);
    }


    @GetMapping("/load/{productId}")
    public ResultVo<ProductInfo> load(@PathVariable("productId")String productId){
        ProductInfo productInfo = productInfoService.findOne(productId);
        if (productInfo!=null){
            return ResultVoUtil.success(productInfo);
        }else
            return ResultVoUtil.error(ResultEnum.PRODUCT_NOT_EXIST.getCode(),ResultEnum.PRODUCT_NOT_EXIST.getMsg());
    }



    /**
     * 查询所有上架商品
     * @return
     */
    @GetMapping("/category/list")
    @SentinelResource(value = "productCategoryList",fallback = "CallTimeNoArg",fallbackClass = SellFackHandler.class)
    public ResultVo List(){

        //查询所有的上架商品
        List<ProductInfo> productInfoList = productInfoService.findUp();
        //查询这些商品所带的类目
        List<Integer> categoryTypeList = productInfoList.stream()
                .map(e->e.getCategoryType())
                .collect(Collectors.toList());
        //拼装
        List<ProductCategory> productCategoryList = categoryFeign.findByCategoryTypeIn(categoryTypeList);

        List<ProductVo> productVoList = new ArrayList<>();
        for (ProductCategory productCategory:productCategoryList){
            ProductVo productVo = new ProductVo();
            /**
             * 前面的对象值复制到后面进去，注意空值也会复制
             */
            BeanUtils.copyProperties(productCategory,productVo);

            List<ProductInfoVo> productInfoVoList = new ArrayList<>();
            for (ProductInfo productInfo:productInfoList){
                if (productInfo.getCategoryType()==productCategory.getCategoryType()){
                    ProductInfoVo productInfoVo = new ProductInfoVo();
                    BeanUtils.copyProperties(productInfo,productInfoVo);
                    productInfoVoList.add(productInfoVo);
                }
            }
            productVo.setCategoryFoods(productInfoVoList);
            productVoList.add(productVo);
        }

        return ResultVoUtil.success(productVoList);
    }
}
